﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLVMBAYDomain;
using QLVMBAYDatacess;
using System.Data;


namespace QLVMBAY.Admin
{
    public partial class User : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                            
                LoadGrid();
            }

        }
        private void LoadGrid()
        {            
            grdUser.DataSource =NguoiDungDA.Inst.GetAll(false);
            grdUser.DataBind();
            if (grdUser.PageCount <= 1)
            {
                grdUser.PagerStyle.Visible = false;
            }
        }
        private void ResetControlValues()
        {
            txtName.Text = "";
            txtPassword.Text = "";
            txtUsername.Text = "";
            DropLND.SelectedIndex = -1;
        }
        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            ResetControlValues();
            pnView.Visible = false;
            Insert = true;
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                NguoiDung ND = new NguoiDung();
                ND.HoTenND=txtName.Text;
                ND.MatKhau=txtPassword.Text;
                ND.MaNguoiDung =txtUsername.Text;
                ND.LoaiND =(LOAI_NGUOI_DUNG) DropLND.SelectedIndex;
                ND.MaHanhKhach = "0";
                if (Insert == true)
                {
                    NguoiDungDA.Inst.Insert(ND);
                }
                else
                {
                    NguoiDungDA.Inst.Update(ND);
                }
                LoadGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;                
                Insert = false;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;           
            Insert = false;
        }

        protected void grdUser_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var itemUser = NguoiDungDA.Inst.GetByPrimaryKey(false, e.Item.Cells[0].Text);
            switch (e.CommandName)
            {                    
                case "Edit":
                    Insert = false;                
                    txtName.Text = itemUser.HoTenND.ToString();                    
                    txtUsername.Text = itemUser.MaNguoiDung.ToString() ;
                    txtUsername.ReadOnly = true;
                    txtPassword.Text = itemUser.MatKhau.ToString() ;
                    DropLND.SelectedValue = ((int)itemUser.LoaiND).ToString();                    
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;

                case "Delete":
                    NguoiDungDA.Inst.Delete(itemUser);                    
                    LoadGrid();
                    break;
            }

        }       

        protected void grdUser_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdUser.CurrentPageIndex = e.NewPageIndex;
            LoadGrid();
        }

        protected void grdUser_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                var lbl = e.Item.FindControl("lblLoaiND") as Label;
                lbl.Text = ((NguoiDung)e.Item.DataItem).LoaiND.ToString().ToUpperInvariant();

            }
        }
        
    }
}